package com.hula.onion.book.server.spider.platforms.jbb;

import com.alibaba.fastjson.JSONObject;
import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.SpiderCommon;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.utils.CookieUtils;
import com.hula.onion.book.server.utils.HttpUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.protocol.HttpClientContext;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Description:金宝保
 * <p>
 * Create Date:2018/1/30 19:33
 *
 * @author liangbing
 * @version 1.0
 */
@Component("jBBSpider")
public class JBBRSpider extends SpiderCommon {

    /**
     * 登录页面URL
     */
    private static final String LOGIN_PAGE_URL = "https://www.5djbb.com/login";

    /**
     * 登录URL
     */
    private static final String LOGIN_URL = "https://www.5djbb.com/login";

    /**
     * 获取验证码地址URL
     */
    private static final String GET_CAPTCHA_URL_URL = "https://www.5djbb.com/login/default/captcha/refresh/1.html";

    /**
     * 验证码URL
     */
    private static String CAPTCHA_URL = "https://www.5djbb.com/login/default/captcha/v/";

    /**
     * 获取账户信息URL
     */
    private static final String GET_ACCOUNT_INFO_URL = "https://www.5djbb.com/member/center/myAccount.html";

    @Autowired
    private JBBSpiderHelper spiderHelper;

    /**
     * 执行爬取操作
     */
    @Override
    public CommonResponse execute() {

        //1. 从缓存中获取cookie，设置到cookie store中
        CookieUtils.setCookie(loginForm.getPlatformId(), loginForm.getUserId(), getHttpContext().getCookieStore());

        //模拟登陆
        String loginStr = HttpUtils.post(LOGIN_URL,
                spiderHelper.installRequestParams(loginForm),
                getHttpContext());
        if (StringUtils.isEmpty(loginStr)) {
            return new CommonResponse(ResponseStatusEnum.FAIL);
        }

        logger.info("[JBBRSpider] Http response result:{}", loginStr);

        Document dc = Jsoup.parse(loginStr);
        Element element = dc.selectFirst("#interface");
        //登陆成功
        if (element == null) {

            // 爬取数据
            String html = HttpUtils.get(GET_ACCOUNT_INFO_URL, getHttpContext());
            return spiderHelper.parseAccountBalance(html);
        } else {
            CommonResponse commonResponse = new CommonResponse(ResponseStatusEnum.ACCOUNT_ERROR);
            commonResponse.setMsg(element.text());
            return commonResponse;
        }
    }

    @Override
    public String getCaptchaURL() {
        return CAPTCHA_URL;
    }

    @Override
    public String getLoginPageURL() {
        return LOGIN_PAGE_URL;
    }

    /**
     * 金宝保需要请求接口来获取验证码
     */
    public void beforeGetImg(HttpClientContext httpClientContext) {

        String msg = HttpUtils.get(GET_CAPTCHA_URL_URL, httpClientContext);

        if (!StringUtils.isEmpty(msg)) {

            String url = JSONObject.parseObject(msg).getString("url");

            CAPTCHA_URL = "https://www.5djbb.com/" + url;
        }

    }
}

